<template>
  <app-scroll-view @query="queryList" @loadNext="loadNext" occupy-height="222px">
    <template #search>
      <div class="search">
        <el-input
            v-model="listParams.username"
            placeholder="请输入账号名称"
            clearable
            @clear="queryList"
            style="width: 100%;"
        >
          <template #append>
            <el-button @click="queryList">
              <el-icon>
                <search/>
              </el-icon>
            </el-button>
          </template>
        </el-input>
      </div>
    </template>
    <div v-loading="listLoading">
      <div class="user-item" @click="openChat(item)" :class="{'active':$store.state.customer.activeUser&&item.username===$store.state.customer.activeUser.username}"
           :key="item.id" v-for="item in list">
        <span style="flex: 1">
          <app-avatar :size="30" :src="item.avatarUrl"></app-avatar>
        </span>
        <span :title="item.username" style="color: #1394ea">{{ item.username }}</span>
        <span :title="item.nickName">{{ item.nickName }}</span>
      </div>
    </div>
  </app-scroll-view>
</template>

<script lang="ts" setup>
import AppScrollView from '@/components/ScrollView/index.vue'
import {onMounted, reactive, ref} from "vue";
import {getUserList} from "@/api/system";
import {IUserInfo, UserListParams} from "@/api/types/user";
import {store} from "@/store";

const list = ref<IUserInfo[]>([]);
const listCount = ref(0);
const listLoading = ref(false);
const listParams = reactive<UserListParams>({
  pageNum: 1,
  pageSize: 50,
  username: '',
});

const loadList = async () => {
  listLoading.value = true;
  const data = await getUserList(listParams).finally(() => {
    listLoading.value = false;
  });
  list.value.push(...data.rows);
  listCount.value = data.total;
};

const loadNext = (e: any) => {
  if (!listLoading.value) {
    // 判断是否可以加载下一页
    if (listCount.value > list.value.length) {
      listParams.pageNum++
      loadList()
    }
  }
}

const queryList = () => {
  listParams.pageNum = 1
  list.value = []
  loadList()
}

// 打開窗口
const openChat = (item : IUserInfo) => {
  store.commit('customer/setActiveUser',item)
}

onMounted(() => {
  loadList();
});

</script>

<style lang="scss" scoped>
div.content {
  height: 700px;
  overflow: auto;
}

div.user-item {
  display: flex;
  justify-content: space-around;
  align-items: center;
  width: 100%;
  height: 40px;
  cursor: pointer;
  border-bottom: 1px slategrey dashed;

  &.active {
    background: #b3c5ea !important;
  }

  &:hover {
    background: #d8deea;
  }

  & > span {
    text-align: center;
    flex: 2;
    font-size: 12px;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}


div.search {
  z-index: 100;
  position: relative;
  height: 40px;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #ebedee;
  padding: 4px;
}
</style>
